﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Results.Models;

namespace Results.ViewModels
{
    /// <summary>
    /// Seasons like "2010"
    /// </summary>
    public class SeasonsViewModel : ViewModelBase
    {
        /// <summary>
        /// Seasons like "2010"
        /// </summary>
        public ObservableCollection<SeasonViewModel> Seasons { get; protected set; }

        /// <summary>
        /// Populate with all the unique seasons found the tournaments
        /// </summary>
        /// <param name="tournamentsModel">Source data to build from</param>
        /// <param name="notifySelection">Methods to call if the user selects something</param>
        public SeasonsViewModel(IEnumerable<TournamentModel> tournamentsModel, INotifySelection notifySelection)
        {
            Seasons = new ObservableCollection<SeasonViewModel>();

            foreach (var season in
                from tournamentModel in tournamentsModel
                group tournamentModel by tournamentModel.Season into season
                orderby season.Key descending
                select new { Season = season.Key })
            {
                Seasons.Add(new SeasonViewModel(season.Season, notifySelection));
            }
        }
    }
}
